1//////////////////////////////////////////////////////////////////////
2// LibFile: constants.scad
3// Useful Constants.
4// To use this, add the following line to the top of your file.
5// ```
6// include <BOSL/constants.scad>
7// ```
8//////////////////////////////////////////////////////////////////////
9
10/*
11BSD 2-Clause License
12
13Copyright (c) 2017, Revar Desmera
14All rights reserved.
15
16Redistribution and use in source and binary forms, with or without
17modification, are permitted provided that the following conditions are met:
18
19* Redistributions of source code must retain the above copyright notice, this
20 list of conditions and the following disclaimer.
21
22* Redistributions in binary form must reproduce the above copyright notice,
23 this list of conditions and the following disclaimer in the documentation
24 and/or other materials provided with the distribution.
25
26THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*/
37
38
39// Section: General Constants
40
41PRINTER_SLOP = 0.20; // The printer specific amount of slop in mm to print with to make parts fit exactly. You may need to override this value for your printer.
42
43
44
45// Section: Directional Vectors
46// Vectors useful for `rotate()`, `mirror()`, and `align` arguments for `cuboid()`, `cyl()`, etc.
47
48// Constant: V_LEFT
49// Description: Vector pointing left. [-1,0,0]
50// Example(3D): Usage with `align`
51// cuboid(20, align=V_LEFT);
52V_LEFT = [-1, 0, 0];
53
54// Constant: V_RIGHT
55// Description: Vector pointing right. [1,0,0]
56// Example(3D): Usage with `align`
57// cuboid(20, align=V_RIGHT);
58V_RIGHT = [ 1, 0, 0];
59
60// Constant: V_FWD
61// Description: Vector pointing forward. [0,-1,0]
62// Example(3D): Usage with `align`
63// cuboid(20, align=V_FWD);
64V_FWD = [ 0, -1, 0];
65
66// Constant: V_BACK
67// Description: Vector pointing back. [0,1,0]
68// Example(3D): Usage with `align`
69// cuboid(20, align=V_BACK);
70V_BACK = [ 0, 1, 0];
71
72// Constant: V_DOWN
73// Description: Vector pointing down. [0,0,-1]
74// Example(3D): Usage with `align`
75// cuboid(20, align=V_DOWN);
76V_DOWN = [ 0, 0, -1];
77
78// Constant: V_UP
79// Description: Vector pointing up. [0,0,1]
80// Example(3D): Usage with `align`
81// cuboid(20, align=V_UP);
82V_UP = [ 0, 0, 1];
83
84// Constant: V_ALLPOS
85// Description: Vector pointing right, back, and up. [1,1,1]
86// Example(3D): Usage with `align`
87// cuboid(20, align=V_ALLPOS);
88V_ALLPOS = [ 1, 1, 1]; // Vector pointing X+,Y+,Z+.
89
90// Constant: V_ALLNEG
91// Description: Vector pointing left, forwards, and down. [-1,-1,-1]
92// Example(3D): Usage with `align`
93// cuboid(20, align=V_ALLNEG);
94V_ALLNEG = [-1, -1, -1]; // Vector pointing X-,Y-,Z-.
95
96// Constant: V_ZERO
97// Description: Zero vector. Centered. [0,0,0]
98// Example(3D): Usage with `align`
99// cuboid(20, align=V_ZERO);
100V_ZERO = [ 0, 0, 0]; // Centered zero vector.
101
102
103// Section: Vector Aliases
104// Useful aliases for use with `align`.
105
106V_CENTER = V_ZERO; // Centered, alias to `V_ZERO`.
107V_ABOVE = V_UP; // Vector pointing up, alias to `V_UP`.
108V_BELOW = V_DOWN; // Vector pointing down, alias to `V_DOWN`.
109V_BEFORE = V_FWD; // Vector pointing forward, alias to `V_FWD`.
110V_BEHIND = V_BACK; // Vector pointing back, alias to `V_BACK`.
111
112V_TOP = V_UP; // Vector pointing up, alias to `V_UP`.
113V_BOTTOM = V_DOWN; // Vector pointing down, alias to `V_DOWN`.
114V_FRONT = V_FWD; // Vector pointing forward, alias to `V_FWD`.
115V_REAR = V_BACK; // Vector pointing back, alias to `V_BACK`.
116
117
118
119// Section: Pre-Orientation Alignments
120// Constants for pre-orientation alignments.
121
122
123// Constant: ALIGN_POS
124// Description: Align the axis-positive end to the origin.
125// Example(3D): orient=ORIENT_X
126// cyl(d1=10, d2=5, h=20, orient=ORIENT_X, align=ALIGN_POS);
127// Example(3D): orient=ORIENT_Y
128// cyl(d1=10, d2=5, h=20, orient=ORIENT_Y, align=ALIGN_POS);
129// Example(3D): orient=ORIENT_Z
130// cyl(d1=10, d2=5, h=20, orient=ORIENT_Z, align=ALIGN_POS);
131// Example(3D): orient=ORIENT_XNEG
132// cyl(d1=10, d2=5, h=20, orient=ORIENT_XNEG, align=ALIGN_POS);
133// Example(3D): orient=ORIENT_YNEG
134// cyl(d1=10, d2=5, h=20, orient=ORIENT_YNEG, align=ALIGN_POS);
135// Example(3D): orient=ORIENT_ZNEG
136// cyl(d1=10, d2=5, h=20, orient=ORIENT_ZNEG, align=ALIGN_POS);
137ALIGN_POS = 1;
138
139
140ALIGN_CENTER = 0; // Align centered.
141
142// Constant: ALIGN_NEG
143// Description: Align the axis-negative end to the origin.
144// Example(3D): orient=ORIENT_X
145// cyl(d1=10, d2=5, h=20, orient=ORIENT_X, align=ALIGN_NEG);
146// Example(3D): orient=ORIENT_Y
147// cyl(d1=10, d2=5, h=20, orient=ORIENT_Y, align=ALIGN_NEG);
148// Example(3D): orient=ORIENT_Z
149// cyl(d1=10, d2=5, h=20, orient=ORIENT_Z, align=ALIGN_NEG);
150// Example(3D): orient=ORIENT_XNEG
151// cyl(d1=10, d2=5, h=20, orient=ORIENT_XNEG, align=ALIGN_NEG);
152// Example(3D): orient=ORIENT_YNEG
153// cyl(d1=10, d2=5, h=20, orient=ORIENT_YNEG, align=ALIGN_NEG);
154// Example(3D): orient=ORIENT_ZNEG
155// cyl(d1=10, d2=5, h=20, orient=ORIENT_ZNEG, align=ALIGN_NEG);
156ALIGN_NEG = -1;
157
158
159// CommonCode:
160// orientations = [
161// ORIENT_X, ORIENT_Y, ORIENT_Z,
162// ORIENT_XNEG, ORIENT_YNEG, ORIENT_ZNEG,
163// ORIENT_X_90, ORIENT_Y_90, ORIENT_Z_90,
164// ORIENT_XNEG_90, ORIENT_YNEG_90, ORIENT_ZNEG_90,
165// ORIENT_X_180, ORIENT_Y_180, ORIENT_Z_180,
166// ORIENT_XNEG_180, ORIENT_YNEG_180, ORIENT_ZNEG_180,
167// ORIENT_X_270, ORIENT_Y_270, ORIENT_Z_270,
168// ORIENT_XNEG_270, ORIENT_YNEG_270, ORIENT_ZNEG_270
169// ];
170// axiscolors = ["red", "forestgreen", "dodgerblue"];
171// module text3d(text, h=0.01, size=3) {
172// linear_extrude(height=h, convexity=10) {
173// text(text=text, size=size, valign="center", halign="center");
174// }
175// }
176// module orient_cube(ang) {
177// color("lightgray") cube(20, center=true);
178// color(axiscolors.x) up ((20-1)/2+0.01) back ((20-1)/2+0.01) cube([18,1,1], center=true);
179// color(axiscolors.y) up ((20-1)/2+0.01) right((20-1)/2+0.01) cube([1,18,1], center=true);
180// color(axiscolors.z) back((20-1)/2+0.01) right((20-1)/2+0.01) cube([1,1,18], center=true);
181// for (axis=[0:2], neg=[0:1]) {
182// idx = axis + 3*neg + 6*ang/90;
183// rotate(orientations[idx]) {
184// up(10) {
185// fwd(4) color("black") text3d(text=str(ang), size=4);
186// back(4) color(axiscolors[axis]) text3d(text=str(["X","Y","Z"][axis], ["+","NEG"][neg]), size=4);
187// }
188// }
189// }
190// }
191
192
193// Section: Standard Orientations
194// Orientations for `cyl()`, `prismoid()`, etc. They take the form of standard [X,Y,Z]
195// rotation angles for rotating a vertical shape into the given orientations.
196// Figure(Spin): Standard Orientations
197// orient_cube(0);
198
199ORIENT_X = [ 90, 0, 90]; // Orient along the X axis.
200ORIENT_Y = [ 90, 0, 180]; // Orient along the Y axis.
201ORIENT_Z = [ 0, 0, 0]; // Orient along the Z axis.
202ORIENT_XNEG = [ 90, 0, -90]; // Orient reversed along the X axis.
203ORIENT_YNEG = [ 90, 0, 0]; // Orient reversed along the Y axis.
204ORIENT_ZNEG = [ 0, 180, 0]; // Orient reversed along the Z axis.
205
206
207// Section: Orientations Rotated 90º
208// Orientations for `cyl()`, `prismoid()`, etc. They take the form of standard [X,Y,Z]
209// rotation angles for rotating a vertical shape into the given orientations.
210// Figure(Spin): Orientations Rotated 90º
211// orient_cube(90);
212
213ORIENT_X_90 = [ 90, -90, 90]; // Orient along the X axis, then rotate 90 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
214ORIENT_Y_90 = [ 90, -90, 180]; // Orient along the Y axis, then rotate 90 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
215ORIENT_Z_90 = [ 0, 0, 90]; // Orient along the Z axis, then rotate 90 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
216ORIENT_XNEG_90 = [ 0, -90, 0]; // Orient reversed along the X axis, then rotate 90 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
217ORIENT_YNEG_90 = [ 90, -90, 0]; // Orient reversed along the Y axis, then rotate 90 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
218ORIENT_ZNEG_90 = [ 0, 180, -90]; // Orient reversed along the Z axis, then rotate 90 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
219
220
221// Section: Orientations Rotated 180º
222// Orientations for `cyl()`, `prismoid()`, etc. They take the form of standard [X,Y,Z]
223// rotation angles for rotating a vertical shape into the given orientations.
224// Figure(Spin): Orientations Rotated 180º
225// orient_cube(180);
226
227ORIENT_X_180 = [-90, 0, -90]; // Orient along the X axis, then rotate 180 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
228ORIENT_Y_180 = [-90, 0, 0]; // Orient along the Y axis, then rotate 180 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
229ORIENT_Z_180 = [ 0, 0, 180]; // Orient along the Z axis, then rotate 180 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
230ORIENT_XNEG_180 = [-90, 0, 90]; // Orient reversed along the X axis, then rotate 180 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
231ORIENT_YNEG_180 = [-90, 0, 180]; // Orient reversed along the Y axis, then rotate 180 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
232ORIENT_ZNEG_180 = [ 0, 180, 180]; // Orient reversed along the Z axis, then rotate 180 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
233
234
235// Section: Orientations Rotated 270º
236// Orientations for `cyl()`, `prismoid()`, etc. They take the form of standard [X,Y,Z]
237// rotation angles for rotating a vertical shape into the given orientations.
238// Figure(Spin): Orientations Rotated 270º
239// orient_cube(270);
240
241ORIENT_X_270 = [ 90, 90, 90]; // Orient along the X axis, then rotate 270 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
242ORIENT_Y_270 = [ 90, 90, 180]; // Orient along the Y axis, then rotate 270 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
243ORIENT_Z_270 = [ 0, 0, -90]; // Orient along the Z axis, then rotate 270 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
244ORIENT_XNEG_270 = [ 90, 90, -90]; // Orient reversed along the X axis, then rotate 270 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
245ORIENT_YNEG_270 = [ 90, 90, 0]; // Orient reversed along the Y axis, then rotate 270 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
246ORIENT_ZNEG_270 = [ 0, 180, 90]; // Orient reversed along the Z axis, then rotate 270 degrees counter-clockwise on that axis, as seen when facing the origin from that axis orientation.
247
248
249// Section: Individual Edges
250// Constants for specifying edges for `cuboid()`, etc.
251
252EDGE_TOP_BK = [[1,0,0,0], [0,0,0,0], [0,0,0,0]]; // Top Back edge.
253EDGE_TOP_FR = [[0,1,0,0], [0,0,0,0], [0,0,0,0]]; // Top Front edge.
254EDGE_BOT_FR = [[0,0,1,0], [0,0,0,0], [0,0,0,0]]; // Bottom Front Edge.
255EDGE_BOT_BK = [[0,0,0,1], [0,0,0,0], [0,0,0,0]]; // Bottom Back Edge.
256
257EDGE_TOP_RT = [[0,0,0,0], [1,0,0,0], [0,0,0,0]]; // Top Right edge.
258EDGE_TOP_LF = [[0,0,0,0], [0,1,0,0], [0,0,0,0]]; // Top Left edge.
259EDGE_BOT_LF = [[0,0,0,0], [0,0,1,0], [0,0,0,0]]; // Bottom Left edge.
260EDGE_BOT_RT = [[0,0,0,0], [0,0,0,1], [0,0,0,0]]; // Bottom Right edge.
261
262EDGE_BK_RT = [[0,0,0,0], [0,0,0,0], [1,0,0,0]]; // Back Right edge.
263EDGE_BK_LF = [[0,0,0,0], [0,0,0,0], [0,1,0,0]]; // Back Left edge.
264EDGE_FR_LF = [[0,0,0,0], [0,0,0,0], [0,0,1,0]]; // Front Left edge.
265EDGE_FR_RT = [[0,0,0,0], [0,0,0,0], [0,0,0,1]]; // Front Right edge.
266
267// Section: Sets of Edges
268// Constants for specifying edges for `cuboid()`, etc.
269
270EDGES_X_TOP = [[1,1,0,0], [0,0,0,0], [0,0,0,0]]; // Both X-aligned Top edges.
271EDGES_X_BOT = [[0,0,1,1], [0,0,0,0], [0,0,0,0]]; // Both X-aligned Bottom edges.
272EDGES_X_FR = [[0,1,1,0], [0,0,0,0], [0,0,0,0]]; // Both X-aligned Front edges.
273EDGES_X_BK = [[1,0,0,1], [0,0,0,0], [0,0,0,0]]; // Both X-aligned Back edges.
274EDGES_X_ALL = [[1,1,1,1], [0,0,0,0], [0,0,0,0]]; // All four X-aligned edges.
275
276EDGES_Y_TOP = [[0,0,0,0], [1,1,0,0], [0,0,0,0]]; // Both Y-aligned Top edges.
277EDGES_Y_BOT = [[0,0,0,0], [0,0,1,1], [0,0,0,0]]; // Both Y-aligned Bottom edges.
278EDGES_Y_LF = [[0,0,0,0], [0,1,1,0], [0,0,0,0]]; // Both Y-aligned Left edges.
279EDGES_Y_RT = [[0,0,0,0], [1,0,0,1], [0,0,0,0]]; // Both Y-aligned Right edges.
280EDGES_Y_ALL = [[0,0,0,0], [1,1,1,1], [0,0,0,0]]; // All four Y-aligned edges.
281
282EDGES_Z_BK = [[0,0,0,0], [0,0,0,0], [1,1,0,0]]; // Both Z-aligned Back edges.
283EDGES_Z_FR = [[0,0,0,0], [0,0,0,0], [0,0,1,1]]; // Both Z-aligned Front edges.
284EDGES_Z_LF = [[0,0,0,0], [0,0,0,0], [0,1,1,0]]; // Both Z-aligned Left edges.
285EDGES_Z_RT = [[0,0,0,0], [0,0,0,0], [1,0,0,1]]; // Both Z-aligned Right edges.
286EDGES_Z_ALL = [[0,0,0,0], [0,0,0,0], [1,1,1,1]]; // All four Z-aligned edges.
287
288EDGES_LEFT = [[0,0,0,0], [0,1,1,0], [0,1,1,0]]; // All four Left edges.
289EDGES_RIGHT = [[0,0,0,0], [1,0,0,1], [1,0,0,1]]; // All four Right edges.
290EDGES_FRONT = [[0,1,1,0], [0,0,0,0], [0,0,1,1]]; // All four Front edges.
291EDGES_BACK = [[1,0,0,1], [0,0,0,0], [1,1,0,0]]; // All four Back edges.
292EDGES_BOTTOM = [[0,0,1,1], [0,0,1,1], [0,0,0,0]]; // All four Bottom edges.
293EDGES_TOP = [[1,1,0,0], [1,1,0,0], [0,0,0,0]]; // All four Top edges.
294
295EDGES_NONE = [[0,0,0,0], [0,0,0,0], [0,0,0,0]]; // No edges.
296EDGES_ALL = [[1,1,1,1], [1,1,1,1], [1,1,1,1]]; // All edges.
297
298
299// Section: Edge Helpers
300
301EDGE_OFFSETS = [ // Array of XYZ offsets to the center of each edge.
302 [[0, 1, 1], [ 0,-1, 1], [ 0,-1,-1], [0, 1,-1]],
303 [[1, 0, 1], [-1, 0, 1], [-1, 0,-1], [1, 0,-1]],
304 [[1, 1, 0], [-1, 1, 0], [-1,-1, 0], [1,-1, 0]]
305];
306
307
308// Function: corner_edge_count()
309// Description: Counts how many given edges intersect at a specific corner.
310// Arguments:
311// edges = Standard edges array.
312// v = Vector pointing to the corner to count edge intersections at.
313function corner_edge_count(edges, v) =
314 (v[2]<=0)? (
315 (v[1]<=0)? (
316 (v[0]<=0)? (
317 edges[0][2] + edges[1][2] + edges[2][2]
318 ) : (
319 edges[0][2] + edges[1][3] + edges[2][3]
320 )
321 ) : (
322 (v[0]<=0)? (
323 edges[0][3] + edges[1][2] + edges[2][1]
324 ) : (
325 edges[0][3] + edges[1][3] + edges[2][0]
326 )
327 )
328 ) : (
329 (v[1]<=0)? (
330 (v[0]<=0)? (
331 edges[0][1] + edges[1][1] + edges[2][2]
332 ) : (
333 edges[0][1] + edges[1][0] + edges[2][3]
334 )
335 ) : (
336 (v[0]<=0)? (
337 edges[0][0] + edges[1][1] + edges[2][1]
338 ) : (
339 edges[0][0] + edges[1][0] + edges[2][0]
340 )
341 )
342 );
343
344
345
346// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap